﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.MasterData
{
    public partial class frmManageProductionType : BaseForm.frmBaseMdiBusinessForm
    {
        #region vars
        private MstProductTypeEntity mSelectedProductType;
        #endregion

        #region ctors
        public frmManageProductionType()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbCategory, "CG", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCATEGORY, "CG", true);
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtParentType.Tag != null && txtParentType.Tag.GetType() == typeof(MstProductTypeEntity))
            {
                MstProductTypeEntity parent = (MstProductTypeEntity)txtParentType.Tag;
                condition += " AND PT1.PARENT_TYPE_ID = '" + parent.ProductTypeId + "' ";
            }
            if (string.IsNullOrEmpty(txtTypeCode.Text) == false)
            {
                condition += " AND PT1.PRODUCT_TYPE_CODE LIKE '%" + txtTypeCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtTypeName.Text) == false)
            {
                condition += " AND PT1.PRODUCT_TYPE_NAME LIKE '%" + txtTypeName.Text + "%' ";
            }
            if (string.IsNullOrEmpty(cmbCategory.Text) == false && cmbCategory.SelectedValue != null)
            {
                condition += " AND PT1.CATEGORY = '" + cmbCategory.SelectedValue + "' ";
            }
            if (string.IsNullOrEmpty(txtDescription.Text) == false)
            {
                condition += " AND PT1.DESCRIPTION LIKE '%" + txtDescription.Text + "%' ";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "PRODUCT_TYPE_CODE";
            DataTable dt = MstProductTypeBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region events
        private void txtParentType_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductionTypeQuery frm = new frmMaintainProductionTypeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProductType != null)
            {
                txtParentType.Text = frm.SelectedProductType.ProductTypeName;
                txtParentType.Tag = frm.SelectedProductType;
            }
            else
            {
                txtParentType.Text = "";
                txtParentType.Tag = null;
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            mSelectedProductType = new MstProductTypeEntity(gridView1.GetDataRow(e.RowHandle));
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mSelectedProductType == null)
            {
                MessageBox.Show("请选择一个物品类别！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainProductionType frm = new frmMaintainProductionType();
            frm.ThisActionType = ActionType.View;
            frm.SelectedProductType = mSelectedProductType;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedProductType == null)
            {
                MessageBox.Show("请选择一个物品类别！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中物品类别？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = false;
                if (mSelectedProductType.Category == ProductTypeCategory.CG03.ToString())
                {
                    List<MstProductEntity> listProduct = MstProductBs.GetProductByType(mSelectedProductType.ProductTypeId);
                    if (listProduct != null && listProduct.Count > 0)
                    {
                        MessageBox.Show("已经存在此类的物品，不能删除！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    successful = MstProductTypeBs.DeleteMstProductType(mSelectedProductType);
                }
                else
                {
                    dr = MessageBox.Show("此类可能包含子类，如果删除，则子类将一并删除，是否确定删除？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.No)
                    {
                        return;
                    }
                    successful = MstProductTypeBs.DeleteProductType(mSelectedProductType);
                }
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedProductType == null)
            {
                MessageBox.Show("请选择一个物品类别！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainProductionType frm = new frmMaintainProductionType();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedProductType = mSelectedProductType;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainProductionType frm = new frmMaintainProductionType();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }
        #endregion

        private void btnExport_Click_1(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(sfd.FileName);
            }
        }

    }
}
